﻿#include "ui_setCluster.h"
#include "ui_ui_setCluster.h"
#include <QDebug>
#include <QMessageBox>

uiSetCluster::uiSetCluster(Cluster * clus, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::uiSetCluster)
{
    ui->setupUi(this);

    this->m_xml = new ToolXml;

    this->m_clus = clus;

    ui->sn->setText(clus->sn());
    ui->switchId->setText((clus->switchId()));
    ui->line->setText(clus->line());
    ui->lineId->setText(clus->lineId());
    ui->logLevel->setCurrentIndex(clus->logLevel());

    ui->dhcp->setCurrentIndex(clus->dhcp());
    ui->ip->setText(clus->ip());
    ui->mask->setText(clus->mask());
    ui->gw->setText(clus->gw());
    ui->dns0->setText(clus->dns1());
    ui->dns1->setText(clus->dns2());

    if (clus->dhcp() == 0)
    {
        ui->ip->setEnabled(false);
        ui->mask->setEnabled(false);
        ui->gw->setEnabled(false);
    }
}

uiSetCluster::~uiSetCluster()
{
    qDebug() << "uiSetCluster 析构！";
    delete ui;
    delete this->m_xml;
}

void uiSetCluster::on_dhcp_currentIndexChanged(int index)
{
    if (index == 0)
    {
        ui->ip->setEnabled(false);
        ui->mask->setEnabled(false);
        ui->gw->setEnabled(false);
    }
    else if (index == 1)
    {
        ui->ip->setEnabled(true);
        ui->mask->setEnabled(true);
        ui->gw->setEnabled(true);
    }
}

void uiSetCluster::on_pushButton_clicked()
{
//    bool ok;
//    bool ret = true;

    Cluster clus = *this->m_clus;

    clus.setLine(ui->line->text());
    clus.setLineId(ui->lineId->text());
    clus.setLogLevel(ui->logLevel->currentIndex());
    clus.setDhcp(ui->dhcp->currentIndex());
    if (clus.dhcp() == 1)
    {
        clus.setIp(ui->ip->text());
        clus.setMask(ui->mask->text());
        clus.setGw(ui->gw->text());
    }
    clus.setDns1(ui->dns0->text());
    clus.setDns2(ui->dns1->text());

//    if (!ret)
//    {
//        QMessageBox::warning(this, "参数错误", "设置的参数有误！");
//        return;
//    }

    QString xml = m_xml->makeSetClusterXml(&clus);

    emit setCluster(xml);
}
